package com.city.parking;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.CityInfo;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.city.parking.map.OfflineMapSettings;
import com.city.parking.util.SharePrefUtil;
import com.city.parking.util.Utils;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class SearchActivity extends FragmentActivity implements
        OnClickListener, OnGetPoiSearchResultListener{
	private EditText whereEdt;
	private SearchResultAdapter mAdapter;
	private PoiSearch mPoiSearch = null;
	private PoiCitySearchOption mSearchOption;
	private PoiResult mPoiResult;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_search);
		mPoiSearch = PoiSearch.newInstance();
		mPoiSearch.setOnGetPoiSearchResultListener(this);
		mSearchOption = new PoiCitySearchOption();

		ImageView back = (ImageView) findViewById(R.id.nav_ico_back);
		Button searchBtn = (Button) findViewById(R.id.nav_search);
		whereEdt = (EditText) findViewById(R.id.nav_where);
		back.setOnClickListener(this);
		searchBtn.setOnClickListener(this);

		ListView listView = (ListView) findViewById(R.id.search_result_list);
		mAdapter = new SearchResultAdapter(this);
		listView.setAdapter(mAdapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
			        long arg3) {
				// TODO Auto-generated method stub

				PoiInfo poi = (PoiInfo)mAdapter.getItem(pos);
				if (poi != null) {
					Intent intent = getIntent();
					Bundle data = new Bundle();
					data.putDouble("lat", poi.location.latitude);
					data.putDouble("lng", poi.location.longitude);
					intent.putExtras(data);
					setResult(RESULT_OK, intent);
				}
				finish();
			}

		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.nav_ico_back:
			finish();
			break;
		case R.id.nav_search:
			search();
			break;

		default:
			break;
		}
	}

	
	private void search() {
		String keyword = whereEdt.getText().toString();
		if (keyword == null || keyword.equals("")) {
			Toast.makeText(this, "请输入关键字", Toast.LENGTH_SHORT).show();
			return;
		}
		
		String city = ParkingApp.get().mCurCity;
		if (city == null || city.equals("")) {
			city = "南京";
		}

		mSearchOption.city(city);
		mSearchOption.keyword(keyword);
		mSearchOption.pageCapacity(50);
		mSearchOption.pageNum(0);

		mPoiSearch.searchInCity(mSearchOption);
        ParkingApp.get().showProgressBar(this);
	}

	@Override
	public void onResume() {
		super.onResume();
		whereEdt.requestFocusFromTouch();
	}
	
	@Override
	public void onPause() {
		super.onPause();
        ParkingApp.get().hideProgressBar();
	}

	private class ViewHolder {
		TextView name;
		TextView desc;
	}

	private class SearchResultAdapter extends BaseAdapter {
		private LayoutInflater inflater = null;

		public SearchResultAdapter(Context context) {
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			if (mPoiResult == null)
				return 0;
			return mPoiResult.getAllPoi().size();
		}

		@Override
		public Object getItem(int position) {
			if (mPoiResult == null || mPoiResult.getAllPoi().size() == 0)
				return null;
			return mPoiResult.getAllPoi().get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = inflater.inflate(R.layout.search_result_item, null);
				holder.name = (TextView) convertView
				        .findViewById(R.id.item_name);
				holder.desc = (TextView) convertView
				        .findViewById(R.id.item_desc);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			PoiInfo poi = (PoiInfo)getItem(position);
			if (poi != null) {
				holder.name.setText(poi.name);
				holder.desc.setText(poi.address);
			}
			return convertView;
		}
	}

	@Override
    public void onGetPoiDetailResult(PoiDetailResult arg0) {
	    // TODO Auto-generated method stub
	    
    }

	@Override
    public void onGetPoiResult(PoiResult result) {
	    // TODO Auto-generated method stub
        ParkingApp.get().hideProgressBar();
		if (result == null
				|| result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
			Toast.makeText(this, "未找到结果", Toast.LENGTH_LONG)
			.show();
			return;
		}
		if (result.error == SearchResult.ERRORNO.NO_ERROR) {
			mPoiResult = result;
			mAdapter.notifyDataSetChanged();
			return;
		}
		if (result.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) {

			// 当输入关键字在本市没有找到，但在其他城市找到时，返回包含该关键字信息的城市列表
			String strInfo = "在";
			for (CityInfo cityInfo : result.getSuggestCityList()) {
				strInfo += cityInfo.city;
				strInfo += ",";
			}
			strInfo += "找到结果";
			Toast.makeText(this, strInfo, Toast.LENGTH_LONG)
					.show();
		}
    }
}
